package com.lzy.mall.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.product.dao.PmsBrandDao;
import com.lzy.mall.product.dao.PmsCategoryDao;
import com.lzy.mall.product.entity.PmsBrand;
import com.lzy.mall.product.entity.PmsCategoryBrandRelation;
import com.lzy.mall.product.dao.PmsCategoryBrandRelationDao;
import com.lzy.mall.product.service.PmsCategoryBrandRelationService;
import com.lzy.mall.common.utils.PageParaUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 品牌分类关联(PmsCategoryBrandRelation)表服务实现类
 *
 * @author lzy
 * @since 2021-08-25 11:41:13
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsCategoryBrandRelationServiceImpl implements PmsCategoryBrandRelationService {
    private final PmsCategoryBrandRelationDao pmsCategoryBrandRelationDao;

    private final PmsBrandDao brandDao;
    private final PmsCategoryDao categoryDao;

    @Override
    public void updateBrand(Long brandId, String name) {
        List<PmsCategoryBrandRelation> select = pmsCategoryBrandRelationDao.createLambdaQuery()
                .andEq(PmsCategoryBrandRelation::getBrandId, brandId)
                .select();
        select.forEach(a -> a.setBrandName(name));
        pmsCategoryBrandRelationDao.getSQLManager()
                .updateBatchTemplateById(PmsCategoryBrandRelation.class,select);
    }

    @Override
    public List<PmsCategoryBrandRelation> cateLogList(Long brandId) {
        return pmsCategoryBrandRelationDao.createLambdaQuery()
                .andEq(PmsCategoryBrandRelation::getBrandId,brandId)
                .select();
    }

    @Override
    public void save(PmsCategoryBrandRelation categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        if (ObjectUtil.isNull(brandId) || ObjectUtil.isNull(catelogId)){
            // TODO 错误
        }

        String brandName = brandDao.single(brandId).getName();
        String categoryName = categoryDao.single(catelogId).getName();

        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryName);

        pmsCategoryBrandRelationDao.insertTemplate(categoryBrandRelation);
    }

    @Override
    public PmsCategoryBrandRelation getById(Long id) {
        return pmsCategoryBrandRelationDao.single(id);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageQuery pageQuery = PageParaUtils.pagePara(params);

        PageQuery<PmsCategoryBrandRelation> page = pmsCategoryBrandRelationDao
                .createLambdaQuery()
                .page(pageQuery.getPageNumber(), pageQuery.getPageSize());

        return new PageUtils(page);
    }

    @Override
    public void updateById(PmsCategoryBrandRelation categoryBrandRelation) {
        pmsCategoryBrandRelationDao.updateTemplateById(categoryBrandRelation);
    }

    @Override
    public void removeByIds(List<Long> ids) {
        pmsCategoryBrandRelationDao.createLambdaQuery()
                .andIn(PmsCategoryBrandRelation::getId,ids)
                .delete();
    }

    @Override
    public void updateCategory(Long catId, String name) {
        List<PmsCategoryBrandRelation> select = pmsCategoryBrandRelationDao.createLambdaQuery()
                .andEq(PmsCategoryBrandRelation::getCatelogId, catId)
                .select();
        select.forEach(a -> a.setBrandName(name));
        pmsCategoryBrandRelationDao.getSQLManager()
                .updateBatchTemplateById(PmsCategoryBrandRelation.class,select);
    }

    @Override
    public List<PmsBrand> getBrandsByCatId(Long catId) {
        List<PmsCategoryBrandRelation> relations = pmsCategoryBrandRelationDao.createLambdaQuery()
                .andEq(PmsCategoryBrandRelation::getCatelogId, catId)
                .select();
        List<Long> ids = relations.stream()
                .map(PmsCategoryBrandRelation::getBrandId)
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(ids)){
            return null;
        }
        List<PmsBrand> select = brandDao.createLambdaQuery()
                .andIn(PmsBrand::getBrandId, ids)
                .select();
        return select;
    }
}
